package com.coldchain.admin.resoure.api;

import com.coldchain.admin.resoure.entity.Menu;
import com.coldchain.admin.resoure.service.IMenuService;
import com.coldchain.authclient.annotation.CheckUserToken;
import com.coldchain.common.msg.BaseResponse;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.common.resource.annotation.Resource;
import com.coldchain.common.vo.MenuVo;
import com.fangliai.core.context.BaseContextHandler;
import com.fangliai.core.entitiy.SystemModelEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author fangliai
 * @since 2018-08-28
 */
@RestController
@Api(value = "菜单模块",description="系统菜单管理")
@Resource(groupName = "菜单管理" )
@RequestMapping("/menu")
public class MenuController {
    private static Logger log= LoggerFactory.getLogger(MenuController.class);
    @Autowired
    private IMenuService menuService;




    @PostMapping("/edit")
    @CheckUserToken
    @ApiOperation(value="编辑菜单", notes="icon为iconfont字体图标")
    @ApiImplicitParams(value = {
        @ApiImplicitParam(name = "id" ,value = "菜单ID",dataType = "integer",paramType = "query")
    })

    public BaseResponse edit(Menu menu)throws Exception{
        try {
            if(!SystemModelEnum.valueOf(BaseContextHandler.getSysModl()).equals(SystemModelEnum.ADMIN)){
                return  new ObjectRestResponse<>().error("访问受限");
            }
            if(menu.getId()==null){
                menu.setCreateBy(BaseContextHandler.getName());
                menu.setCreateTime(new Date());
            }else {
                menu.setModBy(BaseContextHandler.getName());
                menu.setModTime(new Date());
            }
            menuService.saveOrUpdate(menu);
            return new ObjectRestResponse<>().succeed();
        }catch (Exception e){
            return new ObjectRestResponse<>().error();
        }


    }

    @RequestMapping("/del/{id}")
    @CheckUserToken
    @ApiImplicitParam(name = "id",value = "菜单ID",dataType = "integer",paramType = "path")
    public BaseResponse del(@PathVariable("id") Integer id)throws Exception{
        try {
            if(!SystemModelEnum.valueOf(BaseContextHandler.getSysModl()).equals(SystemModelEnum.ADMIN)){
                return  new ObjectRestResponse<>().error("访问受限");
            }
            menuService.removeById(id);
            return new ObjectRestResponse<>().data("ok");
        }catch (Exception e){
            return new ObjectRestResponse<>().error();
        }

    }


//    @RequestMapping("list/{sysmod}")
//    @ApiOperation(value = "", hidden = true)
//    public ObjectRestResponse<List<Menu>> listAll(@PathVariable("sysmod") Integer sysmod){
//         return  new ObjectRestResponse<>().data(menuService.findMenusBySysmod(sysmod));
//    }


    @RequestMapping("/getMenu/{sysmod}")
    @CheckUserToken
    @ApiOperation(value = "获取系统菜单")
    public ObjectRestResponse<Map<String, MenuVo>> listMenuVoByAll(@PathVariable("sysmod") String sysmod){
        return  new ObjectRestResponse<>().data(menuService.findMenusBySysmod(sysmod));
    }


}
